﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OAclient.Models;
using ServiceApplication.Service.WorkFlow;
using ServiceApplication.Service;
using System.Data;
using CommonLibrary.DataModel.Form;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.Common;
using System.Xml.Linq;
using Bukovics.Workflow.Hosting;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Service.Basic;
using System.Workflow.Activities;
using CommonLibrary.Wf.InterFace;
using System.Workflow.Runtime;
using System.IO;
using System.Xml;
using ServiceApplication.Dao.WorkFlow;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime.Hosting;
using System.Transactions;
using ServiceApplication.Dao.Basic;
using OAclient.Common;
using OAclient.Filters;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

namespace OAclient.Controllers
{
    public class WcdjController : Controller
    {

        private const Int32 PageSize = 10;
        BasicService bs = new BasicService();

        //登记的人
        //wcdj-1
        static DataTable wcdj_list = null;
        public ActionResult WcdjList(UserModel user, string wcdj_date, string SearchStaffOutAddrName, string wcdj_reason, string SearchLxrName, string SearchDhNum, string wcdj_stime,
            string wcdj_pretime, string wcdj_etime, string wcdj_mark, string wcdj_comeback, int? wcdj_idDel, string wcdj_return, int? wcdj_id, int? page)
        {
            //if (wcdj_id == null) wcdj_id = -1;

            //if (wcdj_id != -1)
            //{
            //    wcdj_comeback = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            //    bs.UpdateEtime(wcdj_id.Value, wcdj_comeback);
            //}

            //if (wcdj_idDel == null) wcdj_idDel = -1;

            //if (wcdj_idDel != -1)
            //{
            //    bs.DelectById(wcdj_idDel.Value);
            //}

            if (page == null) page = 1;

            ViewData["date"] = DateTime.Now.ToString("yyyy-MM-dd");

            if (wcdj_date != null)//添加新的一条记录
            {
                WcdjEntity wcdjEntity = new WcdjEntity();
                wcdjEntity.user_id = user.user.user_id;
                wcdjEntity.wcdj_date = wcdj_date;
                wcdjEntity.wcdj_addr = SearchStaffOutAddrName;
                wcdjEntity.wcdj_reason = wcdj_reason;
                wcdjEntity.wcdj_lxr = SearchLxrName;
                wcdjEntity.wcdj_dh = SearchDhNum;
                wcdjEntity.wcdj_stime = wcdj_stime;
                wcdjEntity.wcdj_pretime = wcdj_pretime;
                wcdjEntity.wcdj_mark = wcdj_mark;
                bs.AddNewRegister(wcdjEntity);
                wcdj_id = -1;
            }

            //wcdj_list = bs.QueryWcdjById(user.user.user_id);

            //分页
            //var pageInfo = new PagingInfo
            //{
            //    CurrentPage = page.Value,//第几页
            //    ItemsPerPage = PageSize,//页大小
            //    TotalItems = wcdj_list.Rows.Count//全部的页项
            //};
            //if (wcdj_list.Rows.Count > 0)
            //    wcdj_list = wcdj_list.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();//从第几页开始

            //ViewData["pageInfo"] = pageInfo;
            //ViewData["allInfo"] = wcdj_list;

            //wcdj_id = -1;
            return View();
        }

        string wcdj_comeback;
        public ActionResult ListTable(UserModel user, int? page)
        {
            var wcdj_id = Request.Form["wcdj_id"] ?? Request.QueryString["wcdj_id"];
            var wcdj_idDel = Request.Form["wcdj_idDel"] ?? Request.QueryString["wcdj_idDel"];
            //var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            //var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            //var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            if (page == null)
            {
                page = 1;
            }
            if (wcdj_id == null)
            {
                wcdj_id = "-1";
            }

            if (wcdj_id != "-1")
            {
                wcdj_comeback = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                bs.UpdateEtime(wcdj_id, wcdj_comeback);
            }

            if (wcdj_idDel == null)
            {
                wcdj_idDel = "-1";
            }
            if (wcdj_idDel != "-1")
            {
                bs.DelectById(wcdj_idDel);
            }

            ViewData["date"] = DateTime.Now.ToString("yyyy-MM-dd");

            wcdj_list = bs.QueryWcdjById(user.user.user_id);

            var pageInfo = new PagingInfo
            {
                CurrentPage = page.Value,//第几页
                ItemsPerPage = PageSize,//页大小
                TotalItems = wcdj_list.Rows.Count//全部的页项
            };
            if (wcdj_list.Rows.Count > 0)
                wcdj_list = wcdj_list.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();//从第几页开始

            ViewData["pageInfo"] = pageInfo;
            ViewData["allInfo"] = wcdj_list;

            wcdj_id = "-1";

            return PartialView();
        }

        //管理员登录
        //wcdj-2
        static DataTable wcdj_Mrg = null;
        int? searachId = null;
        //, int? searchclrid_wfcurtask, string mrg_message, string mrg_time, string sure, string wcdj_stime, string wcdj_etime, string wcdj_return, string wcdj_selectchecked
        public ActionResult WcdjMrg(UserModel user, int? page)
        {
            //var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            //var checked_sign = Request.Form["wcdj_selectchecked"] ?? Request.QueryString["checked_sign"];
            //var etime = Request.Form["wcdj_etime"] ?? Request.QueryString["etime"];
            //var stime = Request.Form["wcdj_stime"] ?? Request.QueryString["stime"];
            //var sure = Request.Form["sure"] ?? Request.QueryString["sure"];
            //var mrg_message = Request.Form["mrg_message"] ?? Request.QueryString["mrg_message"];
            //var mrg_time = Request.Form["mrg_time"] ?? Request.QueryString["mrg_time"];

            //if (sure != "" && sure != null)
            //{
            //    bs.UpdateManagerById(sure, mrg_message, mrg_time);
            //}


            //if (stime != null || etime != null)
            //{
            //    if (searachId != null)
            //    {
            //        if (checked_sign == "0")
            //        {
            //            wcdj_Mrg = bs.QueryEntityByIdAndDateRegion(select_id, stime, etime);
            //        }
            //        else
            //        {
            //            wcdj_Mrg = bs.QueryEntityByIdAndDateChecked(select_id, stime, etime);
            //        }
            //    }
            //    else
            //    {
            //        if (checked_sign == "0")
            //        {
            //            wcdj_Mrg = bs.QueryEntityByDate(stime, etime);
            //        }
            //        else
            //        {
            //            wcdj_Mrg = bs.QueryEntityByDateChecked(stime, etime);
            //        }
            //    }
            //}
            //else
            //{
            //    return View();
            //}



            //if (page == null) page = 1;



            //var pageInfo = new PagingInfo
            //{
            //    CurrentPage = page.Value,//第几页
            //    ItemsPerPage = PageSize,//页大小
            //    TotalItems = wcdj_Mrg.Rows.Count//全部的页项
            //};
            //if (wcdj_Mrg.Rows.Count > 0)
            //    wcdj_Mrg = wcdj_Mrg.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();//从第几页开始

            //ViewData["pageInfo"] = pageInfo;
            //ViewData["wcdj_Mrg"] = wcdj_Mrg;

            return View();
        }

        // mrg的查询结果
        // wcdj-2
        public ActionResult MyTable(UserModel user, int? page)
        {
            if (page == null)
            {
                page = 1;
            }

            var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            var checked_sign = Request.Form["wcdj_selectchecked"] ?? Request.QueryString["checked_sign"];
            var etime = Request.Form["wcdj_etime"] ?? Request.QueryString["etime"];
            var stime = Request.Form["wcdj_stime"] ?? Request.QueryString["stime"];
            var sure = Request.Form["sure"] ?? Request.QueryString["sure"];
            var mrg_message = Request.Form["mrg_message"] ?? Request.QueryString["mrg_message"];
            var mrg_time = Request.Form["mrg_time"] ?? Request.QueryString["mrg_time"];


            ViewData["searchclrid_wfcurtask"] = select_id;
            ViewData["wcdj_selectchecked"] = checked_sign;
            ViewData["wcdj_etime"] = etime;
            ViewData["wcdj_stime"] = stime;

            if (sure != "" && sure != null)
            {
                bs.UpdateManagerById(sure, mrg_message, mrg_time);
            }


            if (stime != null || etime != null)
            {
                if (select_id != null && select_id != "")
                {
                    if (checked_sign == "0")
                    {
                        wcdj_Mrg = bs.QueryEntityByIdAndDateRegion(select_id, stime, etime);
                    }
                    else
                    {
                        wcdj_Mrg = bs.QueryEntityByIdAndDateChecked(select_id, stime, etime);
                    }
                }
                else
                {
                    if (checked_sign == "0")
                    {
                        wcdj_Mrg = bs.QueryEntityByDate(stime, etime);
                    }
                    else
                    {
                        wcdj_Mrg = bs.QueryEntityByDateChecked(stime, etime);
                    }
                }
            }
            else
            {
                return PartialView();
            }


            var pageInfo = new PagingInfo
            {
                CurrentPage = page.Value,//第几页
                ItemsPerPage = PageSize,//页大小
                TotalItems = wcdj_Mrg.Rows.Count//全部的页项
            };
            if (wcdj_Mrg.Rows.Count > 0)
                wcdj_Mrg = wcdj_Mrg.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();//从第几页开始

            ViewData["pageInfo"] = pageInfo;
            ViewData["wcdj_Mrg"] = wcdj_Mrg;


            return PartialView();
        }

        //static DataTable wcdj_Mrg2 = null;
        //public ActionResult WcdjMrg2(UserModel user, int? page)
        //{
        //    if (page==null)
        //    {
        //        page = 1;
        //    }

        //    var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
        //    var checked_sign = Request.Form["wcdj_selectchecked"] ?? Request.QueryString["checked_sign"];
        //    var etime = Request.Form["wcdj_etime"] ?? Request.QueryString["etime"];
        //    var stime = Request.Form["wcdj_stime"] ?? Request.QueryString["stime"];

        //    ViewData["searchclrid_wfcurtask"] = select_id;
        //    ViewData["wcdj_selectchecked"] = checked_sign;
        //    ViewData["wcdj_etime"] = etime;
        //    ViewData["wcdj_stime"] = stime;
        //}

        public FileResult WcdjDerive()
        {
            var select_id = Request.Form["searchclrid_wfcurtask"] ?? Request.QueryString["select_id"];
            var checked_sign = Request.Form["wcdj_selectchecked"] ?? Request.QueryString["checked_sign"];
            var etime = Request.Form["wcdj_etime"] ?? Request.QueryString["etime"];
            var stime = Request.Form["wcdj_stime"] ?? Request.QueryString["stime"];
            var sure = Request.Form["sure"] ?? Request.QueryString["sure"];
            var mrg_message = Request.Form["mrg_message"] ?? Request.QueryString["mrg_message"];
            var mrg_time = Request.Form["mrg_time"] ?? Request.QueryString["mrg_time"];
            if (stime != null || etime != null)
            {
                if (select_id != null && select_id != "")
                {
                    if (checked_sign == "0")
                    {
                        wcdj_Mrg = bs.QueryEntityByIdAndDateRegion(select_id, stime, etime);
                    }
                    else
                    {
                        wcdj_Mrg = bs.QueryEntityByIdAndDateChecked(select_id, stime, etime);
                    }
                }
                else
                {
                    if (checked_sign == "0")
                    {
                        wcdj_Mrg = bs.QueryEntityByDate(stime, etime);
                    }
                    else
                    {
                        wcdj_Mrg = bs.QueryEntityByDateChecked(stime, etime);
                    }
                }
            }

            Microsoft.Office.Interop.Excel.Application xlsApp = null;
            Microsoft.Office.Interop.Excel._Workbook xlsBook = null;
            Microsoft.Office.Interop.Excel._Worksheet xlsSheet = null;

            try
            {
                xlsApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                xlsBook = xlsApp.Workbooks.Add(true);
                xlsSheet = (Microsoft.Office.Interop.Excel._Worksheet)xlsBook.ActiveSheet;
                if (wcdj_Mrg == null || wcdj_Mrg.Rows.Count == 0) return null;

                var dt = wcdj_Mrg;
                var colCount = dt.Columns.Count;
                var rowCount = dt.Rows.Count;
                for (int colIndex = 0; colIndex < colCount; colIndex++)
                {
                    xlsApp.Cells[1, colIndex + 1] = dt.Columns[colIndex].Caption;
                }
                for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        xlsApp.Cells[rowIndex + 2, colIndex + 1] = dt.Rows[rowIndex][colIndex].ToString();
                    }
                }

                var filePath = Server.MapPath("~/Temp/");
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);

                xlsApp.Visible = false;
                xlsApp.DisplayAlerts = false;
                xlsApp.ActiveWorkbook.SaveAs(filePath + "wcdj.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlTemplate,
                    null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                xlsBook.Saved = true;
                xlsApp.Workbooks.Close();
                xlsApp.Quit();
                Marshal.ReleaseComObject((Object)xlsSheet);
                xlsSheet = null;
                Marshal.ReleaseComObject((Object)xlsBook);
                xlsBook = null;
                Marshal.ReleaseComObject((Object)xlsApp);
                xlsApp = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();

                return File(new FileStream(filePath + "wcdj.xls", FileMode.Open), "application/octet-stream", "外出登记记录.xls");
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
